var SYSTEM_CONFIG = {
    BINDING_URI: base_url + 'cms/sm/home/binding/',
    EDITPANEL_URI: base_url + 'cms/cp/project/editpanel/',
    COMMIT_URI: base_url + 'cms/sm/home/oncommit/',
    DELETE_URI: base_url + 'cms/sm/home/ondelete/'
};
var SM_SYSTEM = (function() {

    return {
        addEditor: function (Elid){
            addEditorBasic('editor_value_' + Elid);
            $('#system_form_panel_'+Elid+' [name="DataType"]').val('html');
        },
        removeEditor: function (Elid){
            removeEditor('editor_value_' + Elid);
            $('#system_form_panel_'+Elid+' [name="DataType"]').val('text');
        },
        toggleDesc: function(Elid){
            $('#system_form_panel_'+Elid+' [name="Desc"]').toggle();
        },
        onCommit: function(Params, Id, callback) {
            /* Commit data l�n v�o DB, Id>0 l� Update, Id=0 ho?c ko c� l� Insert */
            backend({
                url: SYSTEM_CONFIG.COMMIT_URI,
                data: {
                    Id: Id,
                    Params: Params
                },
                callback: function(rsdata) {
                    if (rsdata.result < 0) {
                        $.jGrowl(rsdata.message, {
                            sticky: true,
                            theme: 'growl-error',
                            header: 'Error!'
                        });
                    } else {
                        $.jGrowl(rsdata.message);
                        if (typeof callback == 'function') {
                            callback();
                        }
                    }
                }
            }).call();
        },
        onInit: function() {

        },
        onAddItem: function() {
            this.onEditItem(0);
        },
        onEditItem: function( Id ) {
            if (PENDING)
                return false;
            $('#jqxviewport').hide();
            $('#edit_panel_insite').show().html('<div class="base_loading" style="display: block;height: 60px"></div>');
            backend({
                url: SYSTEM_CONFIG.EDITPANEL_URI + SYSTEM_CONFIG.TYPE,
                data: {
                    Id: Id
                },
                callback: function(rsdata) {
                    if (rsdata.result < 0) {
                        $('#edit_panel_insite').html('<span style="color:red">' + rsdata.message + '</span>');

                    } else {
                        $('#edit_panel_insite').html(rsdata.htmlreponse);
                        $('#_form_edit_panel select').select2();
                        //                        islc({
                        //                            element:'#edit_panel_insite .select',
                        //                            multiple:true,
                        //                            selectedList:1,
                        //                            height:240,
                        //                        });

                        addEditorFeature('Content');
                        addEditorFeature('Content2');
                    }
                }
            }).call();
        },
        onSave: function(Id) {
            try {
                //if( $('#system_form_panel_'+Id).validationEngine('validate') === false)return false;
                var Params = $('#system_form_panel_' + Id).serializeObject();
                if (Params.DataType == 'html') {
                    Params.Value = CKEDITOR.instances['editor_value_' + Id].getData();
                }
                this.onCommit(Params, Id, function() {
                    if (Id == 0) {

                    }
                });
            } catch (e) {
                bckdialog({
                    type: 'error'
                }).open(e.message);
            }
            return false;
        },
        onCancel: function() {

        },
        onDeleteItem: function(Id) {
            backend_delete(SYSTEM_CONFIG.DELETE_URI, Id, function() {
                _CP.onRefresh();
            });
        },
        onRefresh: function() {

        }
    };
}());